﻿Imports System.Windows.Forms

Public Class frmSelectLayer
    Private GISMap As DotSpatial.Controls.Map

    Public Sub New(ByVal _GISMap As DotSpatial.Controls.Map)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        GISMap = _GISMap
        With cboLayerName.Items
            .Clear()
            For Each lyr In GISMap.GetPolygonLayers
                If Not String.IsNullOrEmpty(lyr.LegendText) Then .Add(lyr.LegendText)
            Next
            'For i As Integer = 0 To GISMap.Layers.Count - 1
            '    If Not String.IsNullOrEmpty(GISMap.Layers(i).LegendText) Then .Add(GISMap.Layers(i).LegendText)
            'Next
        End With
    End Sub

    Private Sub frmSelectLayer_Load(sender As Object, e As EventArgs) Handles Me.Load
        lblInfo.Text = String.Format("The selected data file ({0}) must be mapped to a specific polygon shapefile. Please select the existing layer corresponding to the shapefile, the type of linkage desired, and then (if appropriate) the attribute and data fields which contain either the WRDB Station ID or segment index.", GISForm.AnimationInfo.dsData.FileName)
        If cboLayerName.Items.Count > 0 Then cboLayerName.SelectedIndex = 0
        If cboXParm.Items.Count > 0 AndAlso cboXParm.SelectedIndex = -1 Then cboXParm.SelectedIndex = 0
        If cboYParm.Items.Count > 0 AndAlso cboYParm.SelectedIndex = -1 Then cboYParm.SelectedIndex = 0
        cboArrowColor.Text = "Black"
        Dim fs As DotSpatial.Data.FeatureSet = GetFeatureSet(GISMap, cboLayerName.Text)

        With cboLinkageType.Items
            .Clear()
            Select Case GISForm.AnimationInfo.dsData.DataType
                Case DataSource.clsData.enumDataType.BMD
                    .Add("Segment Name")
                    If fs.DataTable.Columns.Contains("I") AndAlso fs.DataTable.Columns.Contains("J") Then .Add("Parse I,J,K from Segment Name") 'to I and J attributes
                    .Add("Segment Index")
                Case DataSource.clsData.enumDataType.BMD2
                    .Add("Segment Metadata")
                    If fs.DataTable.Columns.Contains("I") AndAlso fs.DataTable.Columns.Contains("J") Then
                        .Add("Parse I,J,K from Segment Name")
                        Dim dt As DataTable = GISForm.AnimationInfo.dsData.GetStationTable
                        If dt.Columns.Contains("I") And dt.Columns.Contains("J") Then
                            .Add("I,J,K from Metadata")
                        End If
                    End If
                    .Add("Segment Index")
                Case Else
                    .Add("WRDB Station ID")
            End Select
        End With
        With GISForm.AnimationInfo
            cboXParm.Items.AddRange(.dsData.PCodes.ToArray)
            cboYParm.Items.AddRange(.dsData.PCodes.ToArray)
            If Not String.IsNullOrEmpty(.DataLayerName) Then cboLayerName.Text = .DataLayerName
            cboAttributeName.Text = .DataAttribute
            cboDataField.Text = .DataField
            Select Case .dsData.DataType
                Case DataSource.clsData.enumDataType.BMD : cboLinkageType.SelectedIndex = Math.Min(cboLinkageType.Items.Count - 1, Choose(.DataLinkageType + 1, 0, 2, 1, 0))
                Case DataSource.clsData.enumDataType.BMD2 : cboLinkageType.SelectedIndex = Math.Min(cboLinkageType.Items.Count - 1, Choose(.DataLinkageType + 1, 0, 3, 1, 2))
                Case Else
                    cboLinkageType.SelectedIndex = 0
            End Select
            chkVectors.Checked = .DataShowVectors
            cboXParm.Text = .DataXParm
            cboYParm.Text = .DataYParm
            nudArrowLength.Value = Math.Min(nudArrowLength.Maximum, .DataArrowLength)
            cboArrowColor.Text = .DataArrowColor.Name
        End With

    End Sub

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click

        With GISForm.AnimationInfo
            .DataLayerName = cboLayerName.Text
            .DataLayer = GISForm.GetLayer(.DataLayerName)
            .DataAttribute = cboAttributeName.Text
            Select Case cboLinkageType.Text
                Case "Segment Name", "WRDB Station ID", "Segment Metadata" : .DataLinkageType = frmGIS.enumLinkageType.StationID
                Case "Segment Index" : .DataLinkageType = frmGIS.enumLinkageType.SegmentNum
                Case "Parse I,J,K from Segment Name" : .DataLinkageType = frmGIS.enumLinkageType.ParseIJK
                Case "I,J,K from Metadata" : .DataLinkageType = frmGIS.enumLinkageType.IJKtoIJK
            End Select
            .DataField = cboDataField.Text
            .DataHandle = GetLayerIndex(GISMap, .DataLayerName)
            .DataShowVectors = chkVectors.Checked
            .DataXParm = cboXParm.Text
            .DataYParm = cboYParm.Text
            .DataArrowLength = nudArrowLength.Value
            .DataArrowColor = Color.FromName(cboArrowColor.Text)
        End With

        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub cboLinkageType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboLinkageType.SelectedIndexChanged

        cboDataField.Items.Clear()
        Select Case GISForm.AnimationInfo.dsData.DataType
            Case DataSource.clsData.enumDataType.BMD
                cboDataField.Items.Add("Segment Name")
            Case DataSource.clsData.enumDataType.BMD2
                With GISForm.AnimationInfo.dsData.GetStationTable.Columns
                    For c = 0 To .Count - 1
                        cboDataField.Items.Add(.Item(c).ColumnName)
                    Next
                End With
            Case Else
                cboDataField.Items.Add("Station_ID")
        End Select
        cboDataField.SelectedIndex = 0
        cboDataField.Enabled = True

        Select Case cboLinkageType.Text
            Case "WRDB Station ID", "Segment Name", "Segment Metadata"
                If cboAttributeName.Items.Contains("STATION_ID") Then
                    cboAttributeName.Text = "STATION_ID"
                Else
                    cboAttributeName.SelectedIndex = 0
                End If
                cboAttributeName.Enabled = True
                cboDataField.SelectedIndex = 0
                cboDataField.Enabled = True
            Case "Segment Index"
                If cboAttributeName.Items.Contains("SEGID") Then
                    cboAttributeName.Text = "SEGID"
                Else
                    cboAttributeName.SelectedIndex = 0
                End If
                cboAttributeName.Enabled = True
                cboDataField.SelectedIndex = -1
                cboDataField.Enabled = False
            Case "Parse I,J,K from Segment Name"
                cboAttributeName.SelectedIndex = -1
                cboAttributeName.Enabled = False
                cboDataField.SelectedIndex = 0
                cboDataField.Enabled = False
            Case "I,J,K from Metadata"
                cboAttributeName.SelectedIndex = -1
                cboAttributeName.Enabled = False
                cboDataField.SelectedIndex = -1
                cboDataField.Enabled = False
        End Select
    End Sub

    Private Sub cboLayer_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboLayerName.SelectedIndexChanged
        If cboLayerName.SelectedIndex = -1 Then Exit Sub
        With cboAttributeName.Items
            .Clear()
            Dim fs As DotSpatial.Data.FeatureSet = GetFeatureSet(GISMap, cboLayerName.Text)
            For i As Integer = 0 To fs.DataTable.Columns.Count - 1
                Dim FieldName As String = fs.DataTable.Columns(i).ColumnName
                .Add(FieldName)
                Dim colType As Type = fs.DataTable.Columns(i).DataType
            Next
            If cboAttributeName.Text = "" And cboAttributeName.Items.Count > 0 Then cboAttributeName.SelectedIndex = 0
        End With
    End Sub

    Private Sub chkVectors_CheckedChanged(sender As Object, e As EventArgs) Handles chkVectors.CheckedChanged
        tblVector.Visible = chkVectors.Checked
    End Sub

End Class
